import Vue from 'vue'
import Vuex from 'vuex'
import ax from './api/axios'
import { setCookie, getCookie, delCookie } from './assets/js/common'

Vue.use(Vuex)

const state = {
    routeList: [],
    adminData: null,
    replyData: null, // 回复评论数据
    reviewData: null, // 书评数据
    adminInfoList: null // 加载管理员列表
}

const mutations = {
    adminData (state, data) {
        state.adminData = data
        if (data) {
            setCookie('adminRole', data.role, 1)
            if (data.role === 0) {
                state.routeList = [
                    { title: '用户管理',
                        children: [
                            { title: '用户列表', url: '/userList/1' }
                        ]
                    },
                    { title: '视频管理', children: [], url: '/movieList/1' }
                ]
            } else if (data.role === 1) {
                state.routeList = [
                    { title: '用户管理',
                        children: [
                            { title: '用户列表', url: '/userList/1' }
                        ]
                    }
                ]
            }
        }
    }
}

const actions = {
    // 校验登录状态
    async adminIsLogin ({ commit }) {
        ax.defaults.headers.common['adminToken'] = getCookie('adminToken') || '0'
        const res = await ax.get('/adminIsLogin')
        commit('adminData', res.data)
        return res
    },

    // 后台管理员登录
    async adminLogin ({ commit }, data) {
        const res = await ax.post('/adminLogin', data)
        if (res.code === 200) {
            setCookie('adminToken', res.data.adminToken, 1)
            ax.defaults.headers.common['adminToken'] = res.data.adminToken
        }
        commit('adminData', res.data)
        return res
    },

    // 后台管理员退出登录
    async adminLoginOut ({ commit }) {
        delCookie('adminToken')
        delCookie('adminRole')
        commit('adminData', null)
        ax.defaults.headers.common['adminToken'] = '0'
        if (getCookie('adminToken')) {
            return false
        } else {
            return true
        }
    },

    // 获取管理员列表
    async getAdminList ({ commit }, data) {
        const res = await ax.get('/getAdminList', { params: data })
        return res
    },

    // 新增管理员
    async addAdminList ({ commit }, data) {
        const res = await ax.post('/addAdminList', data)
        return res
    },

    // 修改管理员
    async updateAdminList ({ commit }, data) {
        const res = await ax.put('/updateAdminList', data)
        return res
    },

    // 删除管理员
    async delAdminList ({ commit }, data) {
        const res = await ax.delete('/delAdminList', { params: data })
        return res
    },

    // 获取视频列表
    async getMovieList ({ commit }, data) {
        const res = await ax.get('/getMovieList', { params: data })
        return res
    },

    // 新增视频
    async addMovieList ({ commit }, data) {
        const res = await ax.post('/addMovieList', data)
        return res
    },

    // 修改视频
    async updateMovieList ({ commit }, data) {
        const res = await ax.put('/updateMovieList', data)
        return res
    },

    // 删除视频
    async delMovieList ({ commit }, data) {
        const res = await ax.delete('/delMovieList', { params: data })
        return res
    },

    // 视频上传
    async uploadMovieVideo ({ commit }, data) {
        const res = await ax.put('/uploadMovieVideo', data)
        return res
    }
}

export default new Vuex.Store({
    state,
    mutations,
    actions
})
